#include "StdAfx.h"
#include "ResManager.h"
#include "Game.h"
#include "DrawWindow.h"
#include "Elements.h"
#include "SelectionSystem.h"


////////////////////////////////////////////////////////////////

void SelectionSystem::MakePreArragedIconsLayout(){
	IconsLayout = G.RM->CreateBufferBmp(W->BBWidth, W->BBHeight);
	W->Put2EmptyDC(IconsLayout);
	HDC tDC = W->CreateEmptyDC();

	HBRUSH alpha = CreateSolidBrush(RGB(255, 0, 255));
	HBRUSH old = (HBRUSH)SelectObject(W->EmptyDC, alpha);
	PatBlt(W->EmptyDC, 0, 0, BkW, BkH, PATCOPY);
	SelectObject(W->EmptyDC, old);
	DEL_OBJ(alpha);

	if(G.FBI->nFighter < IconsPerLine) IconsPerLine = G.FBI->nFighter;

	StartX= (W->BBWidth - (IconsPerLine * IconW + (IconsPerLine -1) * ICONS_INTERVAL))/2;

	Lines= G.FBI->nFighter / IconsPerLine;
	int XSrc = StartX;
	StartY= (W->BBHeight - (Lines * IconH + (Lines -1) * ICONS_INTERVAL))/2;
	int YSrc = StartY;

	for(int i= 1; i <= G.FBI->nFighter; i++){
		SelectObject(tDC, FighterIcon[i]);
		BitBlt(W->EmptyDC, XSrc, YSrc, IconW, IconH, tDC, 0, 0, SRCCOPY);
		if(i % IconsPerLine == 0){
			XSrc = StartX;
			YSrc += (IconH + ICONS_INTERVAL);
		}else{
			XSrc +=  IconW;
			XSrc += ICONS_INTERVAL;
		}
	}

	W->DeleteEmptyDC(tDC);
}

bool SelectionSystem::PlayerSelected(){
	//for debug only:
	if(Selected[0] == 1 && Selected[1] == 1) return true;
	return false;

	//return Selected[0] && Selected[1];
}

void SelectionSystem::SetSelectPosByInput(){

	for(PLR_ID Pid = 0; Pid<nP; Pid.data++)
	{
		if(Selected[Pid]) continue;

		if(G.INP->IsKeyDown_Once(Pid, KEY_UP)){
CASE_KEY_UP:
			if(SelectPos[Pid] .y == 1) SelectPos[Pid].y  = Lines; 
			else SelectPos[Pid].y--;
			goto CASE_END;
		}

		if(G.INP->IsKeyDown_Once(Pid, KEY_DOWN)){
CASE_KEY_DOWN:
			if(SelectPos[Pid] .y == Lines) SelectPos[Pid].y  = 1;
			else SelectPos[Pid].y++;
			goto CASE_END;
		}

		if(G.INP->IsKeyDown_Once(Pid, KEY_LEFT)){
			if(SelectPos[Pid] .x == 1){ SelectPos[Pid].x  = IconsPerLine; goto CASE_KEY_UP;}
			else SelectPos[Pid].x--;
			goto CASE_END;
		}

		if(G.INP->IsKeyDown_Once(Pid, KEY_RIGHT)){
			if(SelectPos[Pid] .x == IconsPerLine){ SelectPos[Pid].x  = 1; goto CASE_KEY_DOWN;}
			else SelectPos[Pid].x++;
			goto CASE_END;
		}

		if(G.INP->IsKeyDown(Pid, KEY_LP)){
			Selected[Pid] = true;
		}//switch

CASE_END:
		SelectedFighterID[Pid] =  SelectPos[Pid].x + (SelectPos[Pid].y-1) * IconsPerLine;

		//char str[100];
		//sprintf(str, "%dP select: %d, %d\n", Pid+1, SelectedFighterID[Pid], __LINE__);
		//OutputDebugStringA(str);

		FramePos[Pid].x = StartX + ((SelectPos[Pid].x -1) * (IconW + ICONS_INTERVAL));
		FramePos[Pid].y = StartY + ((SelectPos[Pid].y -1) * (IconH + ICONS_INTERVAL));
		int FrameOffsetX = (FrameW - IconW)/2;
		int FrameOffsetY = (FrameH - IconH)/2;
		FramePos[Pid].x -=FrameOffsetX;
		FramePos[Pid].y -= FrameOffsetY;
	}
}


void SelectionSystem::LoadBkgnd(){
	hBm = G.RM->LoadBmp(0xB);
	Frame[0] = G.RM->LoadBmp(0xF0);
	Frame[1] = G.RM->LoadBmp(0xF1);
	if(Frame[0])
		GetBitmapSize(Frame[0], FrameW, FrameH);
	_ASSERT(FrameW && FrameH);
}

#define MOVETO(p, a, b) while(*p != a || *(p+1) != b){ while(*p != a && *p != 0) *p ++; if(*p ==0) break;}

void SelectionSystem::LoadFaces(){
	for(int i= 1; i <= G.FBI->nFighter; i++){
		int StrId = 0x100 + i; // 1 ID 10. bmp stand

		WCHAR* str = G.RM->LoadObjStr(StrId);
		if(!str) goto END;
		MOVETO(str, '1', '0');
		if(_wtoi(str) != 10) goto END;

		int ObjId = (StrId << 8) +0x10;

		SO::Move2NextWord(str);
		SO::Move2NextWord(str);
		int nFrame = _wtoi(str);
		FtrFace[i].Load(ObjId, nFrame);

		FtrFace[i].Width *= 2; 
		FtrFace[i].Height *=2;
		FtrFace[i].Y = W->BBHeight - FtrFace[i].Height;

END:
		G.RM->UnloadObjStr(StrId);
	}
}

void SelectionSystem::LoadIcons(){
	if(!G.FBI->nFighter ) return; //no player ..that's not what we talked about

	for(int i= 1; i <= G.FBI->nFighter; i++){
		int id = ((0x100 + i) << 4) + 0x2;
		FighterIcon[i] = G.RM->LoadBmp(id);
	}
	GetBitmapSize(FighterIcon[1], IconW, IconH);
	_ASSERT(IconW && IconH);
}

void SelectionSystem::DrawSelection(){
	for(int i= 0; i < nP; i++){
		if(Selected[i]) continue;
		W->Put2EmptyDC(Frame[i]);
		BitBlt(W->BackBufferDC, FramePos[i].x, FramePos[i].y, FrameW, FrameH, 
			W->EmptyDC, 0, 0, SRCPAINT);
	}
}

void SelectionSystem::DrawIcons(){
	//draw faces

	//draw pre-arraged icon layout
	W->Put2EmptyDC(IconsLayout);
	TransparentBlt(W->BackBufferDC, 0, 0, W->BBWidth, W->BBHeight, 
		W->EmptyDC, 0, 0, W->BBWidth, W->BBHeight, 0x00FF00FF);

}

void SelectionSystem::DrawFaces(){

	int Fid = SelectedFighterID[0];
	FtrFace[Fid].X = 0;
	FtrFace[Fid].IncreaseFrameByTime(G.T->GetTimeSinceStart());
	FtrFace[Fid].Draw(); 

	//player 2 will be drawed on the right and ToLeft.
	Fid = SelectedFighterID[1];
	FtrFace[Fid].FacingLeft();
	//TODO: change color
	FtrFace[Fid].X = W->BBWidth - FtrFace[Fid].Width;
	FtrFace[Fid].IncreaseFrameByTime(G.T->GetTimeSinceStart());
	FtrFace[Fid].Draw(); 
	FtrFace[Fid].FacingLeft();
}


void SelectionSystem::Draw()
{
	W->Put2EmptyDC(hBm);
	BitBlt(W->BackBufferDC, 0, 0, W->BBWidth, W->BBHeight, W->EmptyDC, 0, 0, SRCCOPY);

	DrawFaces();
	DrawIcons();
	SetSelectPosByInput();
	DrawSelection();

	//char str[50] = {0};
	//static int p1x, p1y, p2x, p2y;
	//if( p1x == SelectPos[0].x && 
	//	p1y == SelectPos[0].y &&
	//	p2x == SelectPos[1].x &&
	//	p2y == SelectPos[1].y){

	//}else{
	//	sprintf(str, "1P: %d,%d, 2P: %d, %d\n", SelectPos[0].x, SelectPos[0].y, SelectPos[1].x, SelectPos[1].y);
	//	OutputDebugStringA(str);
	//	p1x = SelectPos[0].x;
	//	p1y = SelectPos[0].y ;
	//	p2x = SelectPos[1].x ;
	//	p2y = SelectPos[1].y;
	//}

}
SelectionSystem::SelectionSystem( ){
	Reset();
}

void SelectionSystem::Reset( ){
	BkW = 768; BkH = 512;
	hBm = 0; Frame[0] = Frame[1] = 0;
	IconW = IconH = 0;
	IconsPerLine = ICONS_PER_LINE;
	Lines = StartX = StartY = 0;
	memset(FighterIcon, 0, nFtrObj*sizeof(HBITMAP));

	SelectPos[0].x =1; SelectPos[0].y = 1;
	SelectPos[1].x =1; SelectPos[1].y = 1;
	Selected[0] = Selected[1] = false;
	SelectedFighterID[0] = 1; SelectedFighterID[1] = 1;
}


void SelectionSystem::UnloadIcons(){
	for(int i= 1; i <= G.FBI->nFighter; i++){
		G.RM->UnloadBmp(FighterIcon[i]);
	}
}

void SelectionSystem::UnloadFaces(){
	for(int i= 1; i <= G.FBI->nFighter; i++){
		FtrFace[i].Unload();
	}
	G.RM->UnloadBmp(IconsLayout);
}

void SelectionSystem::UnloadBkgnd(){
	G.RM->UnloadBmp(hBm);
	G.RM->UnloadBmp(Frame[0]);
	G.RM->UnloadBmp(Frame[1] );
}

void SelectionSystem::LoadAll(){
	LoadBkgnd();
	LoadIcons();
	_ASSERT(FighterIcon[ObjectArrayLast] == 0);
	MakePreArragedIconsLayout();
	UnloadIcons();
	//LoadActiveIcons(); //when the frames on a icon, show a different face.
	LoadFaces();

}


void SelectionSystem::UnloadAll(){
	UnloadBkgnd();
	//UnloadActiveIcons();
	UnloadFaces();
}